package org.yiqixue.accounts.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yiqixue.accounts.constants.TransactionConstants;
import org.yiqixue.accounts.dto.TransactionDto;
import org.yiqixue.accounts.entity.Accounts;
import org.yiqixue.accounts.entity.Customer;
import org.yiqixue.accounts.entity.Transaction;
import org.yiqixue.accounts.exception.ResourceNotFoundException;
import org.yiqixue.accounts.mapper.TransactionMapper;
import org.yiqixue.accounts.repository.AccountsRepository;
import org.yiqixue.accounts.repository.CustomerRepository;
import org.yiqixue.accounts.repository.TransactionRepository;
import org.yiqixue.accounts.service.ITransactionService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TransactionService implements ITransactionService {

    private final TransactionRepository transactionRepository;
    private final CustomerRepository customerRepository;
    private final AccountsRepository accountsRepository;

    @Autowired
    public TransactionService(TransactionRepository transactionRepository, 
                             CustomerRepository customerRepository,
                             AccountsRepository accountsRepository) {
        this.transactionRepository = transactionRepository;
        this.customerRepository = customerRepository;
        this.accountsRepository = accountsRepository;
    }

    /**
     * 为指定手机号对应客户的账户创建一个存款交易
     * @param mobileNumber 客户手机号
     * @param amount 存款金额
     * @param description 交易描述
     * @return 创建的交易DTO
     */
    @Override
    public TransactionDto createDeposit(String mobileNumber, BigDecimal amount, String description) {
        // 根据手机号查找客户
        Customer customer = customerRepository.findByMobileNumber(mobileNumber)
                .orElseThrow(() -> new ResourceNotFoundException("Customer", "mobileNumber", mobileNumber));

        // 查找客户的账户
        Accounts account = accountsRepository.findByCustomerId(customer.getCustomerId())
                .orElseThrow(() -> new ResourceNotFoundException("Account", "customerId", customer.getCustomerId().toString()));

        // 创建新的交易记录
        Transaction transaction = new Transaction();
        transaction.setAccountNumber(account.getAccountNumber());
        transaction.setAmount(amount);
        transaction.setTransactionDate(LocalDateTime.now());
        transaction.setDescription(description);
        transaction.setStatus(TransactionConstants.STATUS_COMPLETED); // 默认设置为完成状态
        transaction.setType(TransactionConstants.DEPOSIT); // 设置为存款类型

        // 保存交易记录
        Transaction savedTransaction = transactionRepository.save(transaction);

        // 返回交易DTO
        return TransactionMapper.mapToTransactionDto(savedTransaction, new TransactionDto());
    }

    /**
     * 根据账户号获取交易历史
     * @param accountNumber 账户号
     * @return 交易列表
     */
    @Override
    public List<TransactionDto> getTransactionsByAccountNumber(Long accountNumber) {
        List<Transaction> transactions = transactionRepository.findByAccountNumber(accountNumber);
        
        return transactions.stream()
                .map(transaction -> TransactionMapper.mapToTransactionDto(transaction, new TransactionDto()))
                .collect(Collectors.toList());
    }

    /**
     * 根据账户号和日期范围获取交易历史
     * @param accountNumber 账户号
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 交易列表
     */
    @Override
    public List<TransactionDto> getTransactionsByAccountNumberAndDateRange(
            Long accountNumber, LocalDateTime startDate, LocalDateTime endDate) {
        List<Transaction> transactions = transactionRepository.findByAccountNumberAndDateBetween(
                accountNumber, startDate, endDate);
        
        return transactions.stream()
                .map(transaction -> TransactionMapper.mapToTransactionDto(transaction, new TransactionDto()))
                .collect(Collectors.toList());
    }
} 